/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import hibernate.Sesion;
import java.awt.Toolkit;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import logica.CapaLogica;
import presentacion.CapaPresentacion;
import presentacion.acceso.Login;
import presentacion.utilidad.ProgressSplashScreen;
import presentacion.utilidad.event_queue.MyEventQueue;

/**
 *
 * @author Administrador
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Toolkit.getDefaultToolkit().getSystemEventQueue().push(
                new MyEventQueue(200));
        
        System.out.println("Hola amiguito");
        
        try {
            UIManager.setLookAndFeel(
                    "com.jtattoo.plaf.mcwin.McWinLookAndFeel");
//            UIManager.setLookAndFeel(
//                    UIManager.getSystemLookAndFeelClassName());

            //Muestra el SplashScreen mientras se carga la base de datos
            spashScreen();            
            
            new Login(null, false).setVisible(true);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

    /**
     * Muestra la pantalla de bienvenida del programa.
     *
     * @throws InterruptedException
     * @throws FileNotFoundException
     */
    private static void spashScreen() {
        //Se recupera el archivo del directorio donde se ecuentra el jar, se normaliza el espacio
        String fileName = new File(CapaPresentacion.class.
                getProtectionDomain().getCodeSource().getLocation().getPath()).
                getParent().replaceAll("%20", " ");

        fileName = CapaLogica.reemplazarUnaBarraInvertidaPorDosBarrasInvertidas(
                fileName + "\\imagenes\\imagen_agile.png");

        try {
            final InputStream imageStream =
            new BufferedInputStream(new FileInputStream(fileName));

            final ProgressSplashScreen splashScreen = new
                    ProgressSplashScreen(imageStream);
            splashScreen.showProgress("Cargando ...");


        final SwingWorker worker = new SwingWorker() {

            @Override
            protected Object doInBackground() throws Exception {
                Sesion.crearSesionBD();
                return null;
            }

            @Override
            protected void done() {
                splashScreen.close();
                splashScreen.showProgress("");
            }
        };
        worker.execute();

        }  catch (FileNotFoundException f) {
           System.err.println(f.getMessage());
           Sesion.crearSesionBD();
        }
    }    
}
